package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"gorm.io/gorm"
	"log"
	"os"
)

var yamlCmd = &cobra.Command{
	Use:   "yaml",
	Short: "生成yaml文件",
	Run: func(cmd *cobra.Command, args []string) {
		force, _ := cmd.Flags().GetBool("force")

		gorm, _ := cmd.Flags().GetBool("gorm")
		if gorm {
			generateGormProfile(force)
			return
		}
		api, _ := cmd.Flags().GetBool("api")
		if api {
			generateApiProfile(force)
			return
		}
		proto, _ := cmd.Flags().GetBool("proto")
		if proto {
			generateProtoProfile(force)
			return
		}
	},
}

func init() {
	yamlCmd.Flags().BoolP("force", "f", false, "Force overwrite")
	yamlCmd.Flags().BoolP("gorm", "g", false, "generate gorm.yaml file")
	yamlCmd.Flags().BoolP("api", "a", false, "generate api.yaml file")
	yamlCmd.Flags().BoolP("proto", "p", false, "generate proto.yaml file")
	rootCmd.AddCommand(yamlCmd)
}

func generateGormProfile(force bool) {
	_, err := os.Stat("gorm.yaml")
	if err == nil && !force {
		return
	}
	gdb, err := conndb()
	if err != nil {
		log.Fatal(err)
	}
	tables, err := gdb.Migrator().GetTables()
	if err != nil {
		log.Fatal(err)
	}
	file, err := os.Create("gorm.yaml")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	content := `onlyModel: false
outPath: ./dao/query
withUnitTest: false
modelPkgName: model
fieldNullable: false
fieldWithIndexTag: false
fieldWithTypeTag: false
fieldCoverable: false
tables:
`

	for _, table := range tables {
		content += fmt.Sprintf("  - name: %s\n", table)
	}
	_, err = file.WriteString(content)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Generated gorm.yaml")
}

func generateApiProfile(force bool) {
	gdb, err := conndb()
	if err != nil {
		log.Fatal(err)
	}
	dbname := gdb.Migrator().CurrentDatabase()
	filename := dbname + "_api.yaml"
	_, err = os.Stat(filename)
	if err == nil && !force {
		return
	}
	generate(gdb, filename, dbname, false)
}

func generateProtoProfile(force bool) {
	gdb, err := conndb()
	if err != nil {
		log.Fatal(err)
	}
	dbname := gdb.Migrator().CurrentDatabase()
	filename := dbname + "_proto.yaml"
	_, err = os.Stat(filename)
	if err == nil && !force {
		return
	}
	generate(gdb, filename, dbname, true)
}

func generate(gdb *gorm.DB, filename, dbname string, isProto bool) {
	tables, err := gdb.Migrator().GetTables()
	if err != nil {
		log.Fatal(err)
	}
	file, err := os.Create(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	outFile := dbname + ".api"
	if isProto {
		outFile = dbname + ".proto"
	}

	file.WriteString("serviceName: " + dbname + "\n")
	if isProto {
		file.WriteString("packageName: " + dbname + "\n")
		file.WriteString("goPackageName: ./" + dbname + "\n")

	}
	file.WriteString("outFile: " + outFile + "\n")

	if !isProto {
		file.WriteString("jwt: false\n")
		file.WriteString("middleware:\n")
		file.WriteString("  - CheckAuth\n")
	}

	file.WriteString("ignoreColumns: \n")
	file.WriteString("  - deleted_at \n")
	file.WriteString("  - version\n")
	file.WriteString("  - mid\n")
	file.WriteString("onlySearch:\n")
	file.WriteString("  - id\n")
	file.WriteString("  - created_at\n")
	file.WriteString("  - updated_at\n")
	file.WriteString("tables:\n")
	for _, table := range tables {
		file.WriteString(fmt.Sprintf("  - %s\n", table))
	}

	fmt.Println("Generated " + filename)
}
